/**
 * Created by LINLA5 on 8/25/2016.
 */
angular.module("order", ["ui.bootstrap"])
    .controller("historyOrderController", function ($scope, $http, $rootScope) {

        //console.log($rootScope.constant);

        $scope.showDetail = function (order) {
            order.showDetail = !order.showDetail;
        };

        $http.get('/order').then(function (res) {
            var data = res.data;
            if (data.success = true) {
                $scope.historicalOrders = data.data;
                for (var i = 0; i < $scope.historicalOrders.length; i++) {
                    if ($scope.historicalOrders[i] == 0) {
                        $scope.historicalOrders[i].statusStr = "商家未接单";
                    } else if ($scope.historicalOrders[i] == 1) {
                        $scope.historicalOrders[i].statusStr = "商家已接单";
                    } else if ($scope.historicalOrders[i] == 2) {
                        $scope.historicalOrders[i].statusStr = "商家已拒单";
                    }
                    if ($scope.historicalOrders[i].status == 2) {
                        $scope.historicalOrders[i].confirmStr = "订单被取消";
                    } else if ($scope.historicalOrders[i].confirm == 0) {
                        $scope.historicalOrders[i].confirmStr = "未确认收货"
                    } else if ($scope.historicalOrders[i].confirm == 1) {
                        $scope.historicalOrders[i].confirmStr = "已确认收货"
                    }
                    if (($scope.historicalOrders[i].complain == null || $scope.historicalOrders[i].complain == undefined) && $scope.historicalOrders[i].grade == -1 && $scope.historicalOrders[i].confirm == 1 && $scope.historicalOrders[i].status == 1) {
                        $scope.historicalOrders[i].evaluate = 0;
                    } else if (($scope.historicalOrders[i].complain == null || $scope.historicalOrders[i].complain == undefined) && $scope.historicalOrders[i].grade != -1 && $scope.historicalOrders[i].confirm == 1 && $scope.historicalOrders[i].status == 1) {
                        $scope.historicalOrders[i].evaluate = 1;
                    } else if ($scope.historicalOrders[i].grade == -1 && $scope.historicalOrders[i].complain != null && $scope.historicalOrders[i].confirm == 1 && $scope.historicalOrders[i].status == 1) {
                        $scope.historicalOrders[i].evaluate = 2;
                    }
                }

            }
        }, function (res) {

        })
    })
    .controller('ModalDemoCtrl', function ($uibModal, $log) {
        var $ctrl = this;
        $ctrl.items = ['item1', 'item2', 'item3'];

        $ctrl.animationsEnabled = true;

        $ctrl.open = function (evaluate, order, type) {
            if (type == 'complaint') {
                var modalInstance = $uibModal.open({
                    animation: $ctrl.animationsEnabled,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'myModalComplaint.html',
                    controller: 'ModalComplaintCtrl',
                    controllerAs: '$ctrl',

                    resolve: {
                        order: function () {
                            return order;
                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $ctrl.selected = selectedItem;
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            } else {
                var modalInstance = $uibModal.open({
                    animation: $ctrl.animationsEnabled,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'myModalGrade.html',
                    controller: 'ModalGradeCtrl',
                    controllerAs: '$ctrl',

                    resolve: {
                        order: function () {
                            return order;
                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $ctrl.selected = selectedItem;
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            }

        };


        $ctrl.openComponentModal = function () {
            var modalInstance = $uibModal.open({
                animation: $ctrl.animationsEnabled,
                component: 'modalComponent',
                resolve: {
                    items: function () {
                        return $ctrl.items;
                    }
                }
            });

            modalInstance.result.then(function (selectedItem) {
                $ctrl.selected = selectedItem;
            }, function () {
                $log.info('modal-component dismissed at: ' + new Date());
            });
        };

        $ctrl.toggleAnimation = function () {
            $ctrl.animationsEnabled = !$ctrl.animationsEnabled;
        };
    })

    //Please note that $uibModalInstance represents a modal window (instance) dependency.
    //It is not the same as the $uibModal service used above.

    .controller('ModalComplaintCtrl', function ($uibModalInstance, $http, $scope, order) {
        var $ctrl = this;
        //$ctrl.items = items;
        //$ctrl.selected = {
        //    item: $ctrl.items[0]
        //};

        $scope.placeholder = 'please input your complaint!';
        $scope.message = "";
        $scope.left = function () {
            return $scope.message.length;
        };

        $ctrl.ok = function () {
            $http.put('/order/' + order._id + '?action=3', {complaint: $scope.message})
                .then(function (res) {
                    var data = res.data;
                    $uibModalInstance.close();
                    if(order.evaluate == 0){
                        order.evaluate = 2;
                    } else if(order.evaluate == 1) {
                        order.evaluate = 3;
                    }
                }, function (res) {
                });
        };

        $ctrl.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    })
    .controller('ModalGradeCtrl', function ($uibModalInstance, $scope, order, $http) {
        var $ctrl = this;
        //$ctrl.items = items;
        //$ctrl.selected = {
        //    item: $ctrl.items[0]
        //};

        $scope.max = 5;
        $scope.ratingVal = 2;
        $scope.readonly = false;
        $scope.onHover = function (val) {
            $scope.hoverVal = val;
        };
        $scope.onLeave = function () {
            $scope.hoverVal = null;
        }
        $scope.onChange = function (val) {
            $scope.ratingVal = val;
        }

        $ctrl.ok = function () {
            $http.put('/order/' + order._id + "?action=2", {grade: $scope.ratingVal})
                .then(function (res) {
                    var data = res.data;
                    if(order.evaluate == 0){
                        order.evaluate = 1;
                    } else if(order.evaluate == 2) {
                        order.evaluate = 3;
                    }
                    $uibModalInstance.close();
                }, function () {

                });

        };

        $ctrl.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    })
    .directive('star', function () {
        return {
            template: '<ul class="rating" ng-mouseleave="leave()">' +
            '<li ng-repeat="star in stars" ng-class="star" ng-click="click($index + 1)" ng-mouseover="over($index + 1)">' +
            '\u2605' +
            '</li>' +
            '</ul>',
            scope: {
                ratingValue: '=',
                max: '=',
                readonly: '@',
                onHover: '=',
                onLeave: '='
            },
            controller: function ($scope) {
                $scope.ratingValue = $scope.ratingValue || 0;
                $scope.max = $scope.max || 5;
                $scope.click = function (val) {
                    if ($scope.readonly && $scope.readonly === 'true') {
                        return;
                    }
                    $scope.ratingValue = val;
                };
                $scope.over = function (val) {
                    $scope.onHover(val);
                };
                $scope.leave = function () {
                    $scope.onLeave();
                }
            },
            link: function (scope, elem, attrs) {
                elem.css("text-align", "center");
                var updateStars = function () {
                    scope.stars = [];
                    for (var i = 0; i < scope.max; i++) {
                        scope.stars.push({
                            filled: i < scope.ratingValue
                        });
                    }
                };
                updateStars();

                scope.$watch('ratingValue', function (oldVal, newVal) {
                    if (newVal) {
                        updateStars();
                    }
                });
                //scope.$watch('max', function (oldVal, newVal) {
                //    if (newVal) {
                //        updateStars();
                //    }
                //});
            }
        };
    })
//complaint modal
//.controller("complaintText",function($scope){
//    $scope.placeholder = 'please input your complaint!'
//    $scope.message="";
//    $scope.left  = function() {return $scope.message.length;};
//
//})
